package com.rj.bd.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Map;

/**
 * Token工具类
 *
 * @author YUKI
 * @date 2024/11/8
 */
public class TokenUtil {
    private static final String SECRET = "y6plziidr4a5prcw6zupgyihrd6o0id2as1238dfda1dafmbm";
    private static final long EXPIRATION = 1800L;


    private TokenUtil() {
    }

    ;

    /**
     * 解析token
     *
     * @author YUKI
     * @date 2024/11/8
     */
    public static Claims parseToken(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJwt(token)
                .getBody();
        return claims;
    }

    /**
     * 创建token
     *
     * @author YUKI
     * @date 2024/11/8
     */
    public String generateToken(Map<String, Object> claims) throws NoSuchAlgorithmException {
        String token = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS256,SECRET)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION))
                .compact();

        return token;
    }
}
